package acm.天梯赛练习2;

import java.util.Objects;
import java.util.PriorityQueue;
import java.util.Scanner;

public class L {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        while (sc.hasNext()) {
            int n = sc.nextInt();
            int id=1;
            PriorityQueue<Node>[] queues=new PriorityQueue[3];
            for (int i = 0; i < queues.length; i++) {
                queues[i]=new PriorityQueue<>(
                        (a,b)->{
                            if(a.priority==b.priority)  //堆的内部是不稳定的，必须要加上这行，让其稳定
                                return a.id-b.id;
                            return b.priority-a.priority;
                        }
                );
            }
            while (n-- > 0) {
                String order=sc.next();
                int a=sc.nextInt()-1;
                switch (order){
                    case "IN":queues[a].add(new Node(id++,sc.nextInt()));break;
                    case "OUT":
                        System.out.println(queues[a].isEmpty()?"EMPTY":queues[a].poll());break;
                }
            }
        }

    }

    static class Node{
        int id;
        int priority;

        public Node(int id, int priority) {
            this.id = id;
            this.priority = priority;
        }

        @Override
        public String toString() {
            return ""+id;
        }

    }
}
